﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Moe_v0._1
{
    class MoeNode
    {
        private Dictionary<string, MoeNode> _outVertexes = new Dictionary<string,MoeNode>();
        private bool _isFinalNode = false;
        private MoeAnswer _answer = null;
        public MoeNode MoveToNext(string word)
        {
            if (_outVertexes != null && _outVertexes.ContainsKey(word))
            {
                return _outVertexes[word];
            }
            else
            {
                return this;
            }
        }

        // Add new vertex to the node and return new node to the user for linked expression.
        public MoeNode AddOutVertex(string word, MoeNode nextNode)
        {
            Debug.Assert(_outVertexes != null);

            if (!_outVertexes.ContainsKey(word))
            {
                _outVertexes.Add(word, nextNode);
            }

            // No matter whether the vertex of 'word' already exist or not, we both return the next
            // "MoeNode" for linked-expression good but need to ensure nextNode is NOT null!!!
            Debug.Assert(nextNode != null);
            return _outVertexes[word];
        }

        public bool IsFinalNode()
        {
            //Debug.Assert((_isFinalNode && _outVertexes == null) 
            //    || (!_isFinalNode && _outVertexes != null));
            // Hard to say.
            return _isFinalNode;
        }

        public void SetAnswer(MoeAnswer answer)
        {
            _answer = answer;
        }

        public string GetAnswer()
        {
            return _answer == null ? MoeAnswer.GetNegativeAnswer() : _answer.SelectAnswer();
        }

        public Dictionary<string, MoeNode> GetAllOutVertex()
        {
            if (MoeDebugger.IsDebugging)
            {
                MoeDebugger.AddDebugInfo("===MoeNoe::GetAllOutVertexes===");
                foreach (var item in _outVertexes)
                {
                    MoeDebugger.AddDebugInfo(item.Key);
                }
                MoeDebugger.AddDebugInfo("-------------------------------");
            }
            return _outVertexes;
        }

        public void SetItFinalNode()
        {
            // _outVertexes = null;  // Hard to say...
            _isFinalNode = true;
        }
    }
}
